<template>
	<div class="login-wrap">
		<div class="ms-login">
			<div class="ms-title">欢迎登录！</div>
			<el-form :model="param" :rules="rules" ref="login" label-width="0px" class="ms-content">
				<el-form-item prop="username">
					<div>用户名</div>
					<el-input v-model="param.phone" style="height: 45px;" placeholder="请输入账号">
					</el-input>
				</el-form-item>
				<el-form-item prop="password">
					<div>密码</div>
					<el-input type="password" style="height: 45px;" placeholder="请输入密码" v-model="param.password"
						@keydown="keydown">
					</el-input>
				</el-form-item>
				<div> <el-checkbox v-model="checked" label="记住我" /></div>
				<div class="login-btn">
					<el-button type="primary" style="height: 50px; border-radius: 10px;"
						@click="submitForm(login)">登录</el-button>
				</div>
			</el-form>
		</div>
	</div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue';
import { useTagsStore } from '../store/tags';
import { useRouter } from 'vue-router';
import { ElMessage } from 'element-plus';
import type { FormInstance, FormRules } from 'element-plus';
import { loginApi } from '../api/index'
const checked = ref<boolean>(false)
interface LoginInfo {
	phone: string;
	password: string;
	identify_type: number;
	scene: number
}
const keydown = (event: any) => {
	if (event.keyCode === 13) {
		submitForm(login.value)

	}
}
const router = useRouter();
const param = ref<LoginInfo>({
	phone: '',
	password: '',
	identify_type: 0,
	scene: 1
});

const rules: FormRules = {
	phone: [
		{
			required: true,
			message: '请输入用户名',
			trigger: 'blur'
		}
	],
	password: [{ required: true, message: '请输入密码', trigger: 'blur' },]
};

onMounted(() => {
	if (localStorage.getItem('usersuper')) {
		let user: any = localStorage.getItem('usersuper');
		param.value = JSON.parse(user)
		checked.value = true
	}
})
const login = ref<FormInstance>();
const submitForm = async (formEl: any) => {
	const res = await loginApi(param.value)
	if (res.data.code === 0) {
		ElMessage.success('登录成功');
		localStorage.setItem('username', param.value.phone)
		localStorage.setItem('token', res.data.data.token)
		router.push('/');
	} else {
		ElMessage.error(res.data.message);
	}
};

const tags = useTagsStore();
tags.clearTags();
</script>

<style scoped>
.login-wrap {
	position: relative;
	width: 100%;
	height: 100%;
	background-image: url(../assets/img/bg.png);
	background-size: 100%;
}

.ms-title {
	width: 100%;
	margin-top: 60px;
	margin-bottom: 40px;
	line-height: 50px;
	text-align: center;
	font-size: 40px;
	color: #050A56;
	font-family: '微软雅黑';
}

.ms-login {
	position: absolute;
	top: 50%;
	width: 550px;
	height: 620px;
	right: 88px;
	margin-top: -310px;
	border-radius: 20px;
	background: #ffffff;
	overflow: hidden;
}

.ms-content {
	padding: 30px 30px;
}

.login-btn {
	margin-top: 60px;
	text-align: center;
}

.login-btn button {
	width: 100%;
	height: 36px;
	margin-bottom: 10px;
}

.login-tips {
	font-size: 12px;
	line-height: 30px;
	color: #fff;
}
</style>
